/**
 * 护工信息列表
 */

<!-- 护理人员信息表格 -->
$(function(){
    var NurseStatus;
    // 人员状态多选框
    layui.config({
        base: '/js/dist/'
    }).extend({
        xmSelect: 'xm-select'
    }).use(['xmSelect'], function(){
        var xmSelect = layui.xmSelect;
        //渲染多选
        NurseStatus = xmSelect.render({
            el: '#status',
            data: [
                {name: '有效待岗', value: '有效待岗'},
                {name: '有效在岗', value: '有效在岗'},
                {name: '无效转出', value: '无效转出'}
            ]
        });
    });

    // Excel导出
    layui.config({//配置并导入excel插件
        base: '/js/layui_exts/'
    }).use([ 'excel', 'layer'], function() {
        var $ = layui.$;
        var layer = layui.layer;
        var excel = layui.excel;
        $('#exportExcel').on('click', function(){
            console.log("触发导出按钮");
            var send_name = $('#name');
            var send_ybid = $('#ybid');
            var send_sbdw = $('#sbdw');
            var send_zylevel = $('#zylevel');
            // var send_status = $('#status');

            var selectArr = NurseStatus.getValue();
            var send_status = [];
            for (var index=0; index < selectArr.length; index++) {
                console.log("send_status的内容：",selectArr[index].value);
                send_status.push(selectArr[index].value);
            };
            send_status=send_status.join();
            console.log("send_status：",send_status);

            layer.confirm('是否确认导出', function(index) {
                $.ajax({
                    url: '/staff/ExportAllInfo'
                    ,method:'post'
                    ,data: {
                        StaffName: send_name.val(),
                        StaffYbid: send_ybid.val(),
                        StaffSbdw: send_sbdw.val(),
                        StaffZylevel: send_zylevel.val(),
                        StaffStatus: send_status
                    }
                    ,dataType: 'json'
                    , success(res) {
                        var data = res;
                        console.log(res);
                        layer.close(index);
                        data = excel.filterExportData(data, [
                            'ybid', 'rylb', 'name', 'gender', 'id_number', 'zylevel'
                            , 'phone_number', 'sbdw', 'sbdwcode', 'fzrq', 'ybbsmrq'
                            , 'fservdate', 'worklength', 'status', 'serv_org', 'lastserv_org'
                            , 'certificate_code'
                        ]);

                        data.unshift({
                            ybid: "医保ID", rylb: "人员类别", name: '姓名', gender: "性别",
                            id_number: "身份证号", zylevel: '职业等级', phone_number: "联系方式", sbdw: "申报单位",
                            sbdwcode: '申报单位机构代码（同结算代码）', fzrq: "发证日期", ybbsmrq: "获得医保标识码日期",
                            fservdate: "首次服务日期", worklength: "工作年限", status: '人员状态', serv_org: "当前护理机构",
                            lastserv_org: "上一家护理机构",certificate_code: "证书编码"
                        });
                        excel.exportExcel(data, '护理人员信息导出--' + new Date().toLocaleString() + '.xlsx', 'xlsx');
                    }
                    , error() {
                        layer.msg("文件导入失败",{icon: 2, time: 1500},function(){location.reload();});

                        // layer.alert('获取数据失败，请检查是否部署在本地服务器环境下');
                    }
                });
            });
        });
    });

    showNursingInfo();
    function showNursingInfo(){
        layui.use(['table','form'], function(){
            var table = layui.table;
            var form = layui.form;
            var tableIns=table.render({
                elem: '#nursingInfoList'
                ,id:'nursing'
                ,url:'/staff/showNursingInfo'
                ,height:410
                ,cellMinWidth: 80//全局定义常规单元格的最小宽度，layui 2.2.1 新增
                ,request:{
                    pageName: 'pn' //页码的参数名称，默认：page
                    ,limitName: 'limit' //每页数据量的参数名，默认：limit
                }

                ,parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": res.list //解析数据列表
                    }
                }
                ,cols: [[
                    // {field:'id', title: '序号', sort: true,fixed:'left'}
                    {field:'name',width:100,title: '姓名',fixed:'left'}
                    ,{field:'gender',title: '性别',sort:true}
                    ,{field:'id_number',  width: 200, title: '身份证号'}
                    ,{field:'zylevel',width:200, title: '职业等级',sort:true}
                    ,{field:'phone_number',width:150, title: '联系方式'}
                    ,{field:'ybid', width:150,title: '医保ID', sort: true}
                    ,{field:'rylb',width:150,title: '人员类别/工种',sort:true}
                    ,{field:'sbdw', width:250,title: '申报单位',sort:true}
                    ,{field:'sbdwcode', width:200,title: '申报单位机构代码',sort:true}
                    ,{field:'certificate_code', width:200,title: '证书编号'}
                    ,{field:'fzrq', width:200,title: '发证日期'}
                    ,{field:'ybbsmrq',width:200, title: '获得医保标识码日期'}
                    ,{field:'fservdate', width:200,title: '首次服务日期',templet : "<div>{{layui.util.toDateString(d.fservdate, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"}
                    ,{field:'worklength',width:200, title: '工作年限'}
                    ,{field:'status', width:200,title: '人员状态',sort:true}
                    ,{field:'serv_org', width:200,title: '护理机构',sort:true}
                    ,{field:'lastserv_org', width:200, title: '上一家护理机构', sort: true}
                    ,{field:'device_hg', width:200, title: '护工设备', sort: true}
                    // ,{field:'history_change_time', width:200, title: '历史变更时间', sort: true}
                    ,{fixed: 'right', toolbar: '#Check_Edit_Del', width: 170, title: '操作'}

                ]]
                ,page:true
            });
            var tableIns1=table.render({
                elem: '#nursingInfoChangedList'
                ,id:'nursingChanged'
                ,url:'/staff/showNursingChangedInfo'
                ,height:410
                ,cellMinWidth: 80//全局定义常规单元格的最小宽度，layui 2.2.1 新增
                ,request:{
                    pageName: 'pn' //页码的参数名称，默认：page
                    ,limitName: 'limit' //每页数据量的参数名，默认：limit
                }

                ,parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": res.list, //解析数据列表
                    }
                }
                ,cols: [[
                    {field: 'name', width:100,title: '姓名',sort:true}
                    , {field: 'ybid', width: 200, title: '医保卡号',sort:true}
                    , {field: 'history_change_time', width: 200, title: '变更时间', sort: true,templet : "<div>{{layui.util.toDateString(d.history_change_time, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"}
                    , {field: 'start_change_date', width: 200, title: '启变日期'}
                    , {field: 'changed_title', width: 200, title: '变更内容名称', sort: true}
                    , {field: 'history_content', width: 300, title: '变更前的内容', sort: true}
                    , {field: 'new_content', width: 300, title: '变更后的内容', sort: true}
                    , { toolbar: '#Check',title: '操作'}
                    // fixed: 'right',
                ]]
                ,page:true
            });

            // 信息页面--搜索功能
            var $ = layui.$, active = {
                reload1: function(){
                    var send_name = $('#name');
                    var send_ybid = $('#ybid');
                    var send_sbdw = $('#sbdw');
                    var send_zylevel = $('#zylevel');

                    var selectArr = NurseStatus.getValue();
                    var send_status = [];
                    for (var index=0; index < selectArr.length; index++) {
                        console.log("send_status的内容：",selectArr[index].value);
                        send_status.push(selectArr[index].value);
                    };
                    send_status=send_status.join();
                    console.log("send_status：",send_status);

                    console.log(send_name.val());
                    console.log(send_ybid.val());
                    console.log(send_sbdw.val());
                    console.log(send_zylevel.val());

                    //执行重载
                    table.reload('nursing', {
                        url: '/staff/searchStaff',
                        method:'post',
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            StaffName: send_name.val(),
                            StaffYbid: send_ybid.val(),
                            StaffSbdw: send_sbdw.val(),
                            StaffZylevel: send_zylevel.val(),
                            StaffStatus: send_status
                        }
                    }, 'data');
                },
                // 变更页面--搜索功能
                reload2: function(){
                    var send_name = $('#nameSearch');
                    var send_ybid = $('#ybidSearch');
                    var send_title = $('#titleSearch');
                    console.log(send_name.val());
                    console.log(send_ybid.val());
                    console.log(send_title.val());

                    //执行重载
                    table.reload('nursingChanged', {
                        url: '/staff/searchChangedInfo',
                        method:'post',
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            nameSearch: send_name.val(),
                            ybidSearch: send_ybid.val(),
                            titleSearch: send_title.val()
                        }
                    }, 'data');
                },
                Add: function(){
                    var Data=document.getElementById("NursingAdd");
                    console.log(Data);
                    console.log("新增案件");
                    var data0=$('#NursingAdd')[0].innerHTML.format(Data);
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: ['基 本 信 息', 'font-size:20px'],
                        closeBtn: 1,
                        area: ['700px', '500px'],
                        shadeClose: true, //开启遮罩关闭
                        btn: ['关闭'],
                        content:data0
                        // $('#ElderDetail')[0].innerHTML.format(data)
                    });
                    form.render('select');
                },
            };
            $('.SearchLoadNursing .searchBtn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            //信息页面监听（查看\编辑\删除）
            table.on('tool(nursing)', function (obj) {
                var data = obj.data;
                if(data.zylevel=="一级"){
                    data.select1="selected";
                    data.select2="";data.select3="";data.select4="";
                }else if(data.zylevel=="二级"){
                    data.select2="selected";
                    data.select1="";data.select3="";data.select4="";
                }else if(data.zylevel=="三级"){
                    data.select3="selected";
                    data.select1="";data.select2="";data.select4="";
                }else if(data.zylevel=="四级"){
                    data.select4="selected";
                    data.select1="";data.select2="";data.select3="";
                }
                if(data.status=="有效待岗"){
                    data.select5="selected";
                    data.select6="";data.select7="";
                }else if(data.status=="有效在岗"){
                    data.select6="selected";
                    data.select5="";data.select7="";
                }else if(data.status=="无效转出"){
                    data.select7="selected";
                    data.select5="";data.select6="";
                }
                console.log(data);
                var data2=$('#NursingDetail')[0].innerHTML.format(data);
                var data3=$('#NursingEdit')[0].innerHTML.format(data);
                console.log(data3);
                if (obj.event === 'detail') {
                    // layer.msg('ID：'+ data.id + ' 的查看操作');
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: ['基 本 信 息', 'font-size:20px'],
                        closeBtn: 1,
                        area: ['700px', '500px'],
                        shadeClose: true, //开启遮罩关闭
                        btn: ['关闭'],
                        content:data2
                        // $('#ElderDetail')[0].innerHTML.format(data)
                    })
                }
                else if (obj.event === 'del') {
                    // System.out.print("选中栏目：id:"+data.id);
                    layer.confirm('是否确认删除？', function (index) {
                        $.ajax({
                            url: '/staff/delStaff',
                            method: 'post',
                            data: {id: data.id},
                            success: function () {
                                obj.del();
                                layer.close(index);
                            },
                            error: function () {
                                layer.msg("删除护工信息出错")
                            }
                        })
                    });
                }
                else if (obj.event === 'edit') {
                    console.log("修改");
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: ['修 改 信 息','font-size:20px;'],
                        closeBtn: 1,
                        area: ['700px', '600px'],
                        shadeClose: true, //开启遮罩关闭
                        content:data3
                    });
                    form.render('select');
                }
            });

            //历史变更页面监听（查看）
            table.on('tool(nursingChanged)', function (obj) {
                var data = obj.data;
                console.log(data);
                var data2=$('#NursingChangedDetail')[0].innerHTML.format(data);
                console.log(data2);
                if (obj.event === 'detail') {
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: ['变更信息', 'font-size:40px'],
                        closeBtn: 1,
                        area: ['700px', '500px'],
                        shadeClose: true, //开启遮罩关闭
                        btn: ['关闭'],
                        content:data2
                    })
                }
            });
        });
    }
})


